/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * Nihongo
 * Copyright (C) Massimiliano Maniscalco 2011, 2012 <massimiliano@massiLinuxBook>
 * 
Nihongo is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Nihongo is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "test_inserimento_numeri.h"

void TestInserimentoNumeri::
run_test ( char *options )
{
	int nr;
	_modo = options[0];
	preparaSelezioneNumeri ( options[2], options[3] );
	_numeroCorrente = _contenuto->begin();
	nr = _contenuto->size();
	_domandeTotLabel->set_text ( Glib::ustring::format ( nr ) );
	_testWin->present ();
	_timer = 0;
	_nrDom = 0;
	_verso = options[1];
	update_window ();
}

void TestInserimentoNumeri::
update_window ( void )
{
	Glib::ustring _domanda;
	_domandaNumLabel->set_text ( Glib::ustring::format ( _nrDom + 1 ) );
	_timerConnection = Glib::signal_timeout ().connect_seconds (sigc::mem_fun (this, &TestInserimentoNumeri::on_timer_fired), 1 );
	_tempoLabel->set_text ( "00 : 00" );
	_domanda = _verso == CIFRE_HIRAGANA ? _numeroCorrente->first : _numeroCorrente->second;
	_domandaLabel->set_text ( _domanda );
	_rispostaEntry->set_text ( "" );
}

void TestInserimentoNumeri::
on_risposta ( char tasto )
{
	Glib::ustring risposta = _rispostaEntry->get_text ();
	Glib::ustring corretta = _verso == CIFRE_HIRAGANA ? _numeroCorrente->second : _numeroCorrente->first;
	bool risultato = ( risposta.raw() == corretta.raw() );
	_timerConnection.disconnect ();
	if ( !risultato )
	{
		Glib::ustring messaggio = "Sbagliato, la risposta giusta è\n<b><big>" + corretta + "</big></b>";
		Gtk::MessageDialog *_alertDialog = new Gtk::MessageDialog ( *_testWin, "", false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
		_alertDialog->set_message ( messaggio, true);
		int res_id = _alertDialog->run();
		delete _alertDialog;
	}
	next_question ();
}

void TestInserimentoNumeri::
next_question ( void )
{
	_timer = 0;
	_nrDom++;
	_numeroCorrente++;
	if ( _numeroCorrente == _contenuto->end() )
		_testWin->hide();
	else
		update_window ();
}

bool TestInserimentoNumeri::
show_result ( void )
{
	bool presented = false;
	return presented;
}

void TestInserimentoNumeri::
preparaSelezioneNumeri ( char nmrd, char nmax )
{
	if ( _modo == MODO_NUMERI_CARD )
	{
		int nrmax, nrgen;
		switch ( nmax )
		{
			case 0:
				nrmax = 10;
				break;
			case 1:
				nrmax = 99;
				break;
			case 2:
				nrmax = 999;
				break;
			case 3:
				nrmax = 9999;
				break;
			case 4:
				nrmax = 99999999;
				break;
		}
		for ( char i = 0; i < nmrd; i++ )
		{
			nrgen = _random->get_int_range ( 0, nrmax + 1  );
			Numero generato;
			generato.first = Glib::ustring::format ( nrgen );
			numeroInHiragana ( generato.second, nrgen );
			_contenuto->push_back ( generato );
		}
	}
	if ( _modo == MODO_NUMERI_QUAN )
	{
		Numero quantita[10] = { Numero("1","ひとつ"), Numero("2","ふたつ"), Numero("3","みっつ"), Numero("4","よっつ"), Numero("5","いつつ"),
			Numero("6","むっつ"), Numero("7","ななつ"), Numero("8","やっつ"), Numero("9","ここのつ"), Numero("10","とお") };
		for ( char i = 0; i < 10; i++ )
			_contenuto->push_back ( quantita[i] );
	}
	if ( _modo == MODO_CALENDARIO )
	{
		Numero calendario[31] = { Numero("一日", "ついたち"), Numero("二日", "ふつか"), Numero("三日", "みっか"), Numero("四日", "よっか"), Numero("五日", "いつか"), Numero("六日", "むいか"), Numero("七日", "なのか"),
			Numero("八日", "ようか"), Numero("九日", "ここのか"), Numero("十日", "とおか"), Numero("十一日", "じゅういちにち"), Numero("十二日", "じゅうににち"), Numero("十三日", "じゅうさんにち"),
			Numero("十四日", "じゅうよっか"), Numero("十五日", "じゅうごにち"), Numero("十六日", "じゅうろくにち"), Numero("十七日", "じゅうななにち"), Numero("十八日", "じゅうはちにち"), Numero("十九日", "じゅうきゅうにち"),
			Numero("二十日", "はつか"), Numero("二十一日", "にじゅういちにち"), Numero("二十二日", "にじゅうににち"), Numero("二十三日", "にじゅうさんにち"), Numero("二十四日", "にじゅうよっか"), Numero("二十五日", "にじゅうごにち"),
			Numero( "二十六日", "にじゅうろくにち"), Numero("二十七日","にじゅうななにち"), Numero("二十八日", "にじゅうはちにち"), Numero("二十九日", "にじゅうきゅうにち"), Numero("三十日", "さんじゅうにち"), Numero("三十一日", "さんじゅういちにち") };
		for ( char i = 0; i < 31; i++ )
			_contenuto->push_back ( calendario[i] );
	}
}

void TestInserimentoNumeri::
numeroInHiragana ( Glib::ustring &hira, int val )
{
	Glib::ustring cifre = Glib::ustring::format ( val );
	int nc = cifre.size();
	if ( nc > 0 )
	{
		int n = 0;
		do
		{
			switch ( nc - n - 1 )
			{
				case 0: // unita'
					if ( cifre[n] == '1' )
						hira.append ( Glib::ustring ( "いち" ) );
					if ( cifre[n] == '2' )
						hira.append ( Glib::ustring ( "に" ) );
					if ( cifre[n] == '3' )
						hira.append ( Glib::ustring ( "さん" ) );
					if ( cifre[n] == '4' )
						hira.append ( Glib::ustring ( "よん" ) );
					if ( cifre[n] == '5' )
						hira.append ( Glib::ustring ( "ご" ) );
					if ( cifre[n] == '6' )
						hira.append ( Glib::ustring ( "ろく" ) );
					if ( cifre[n] == '7' )
						hira.append ( Glib::ustring ( "なな" ) );
					if ( cifre[n] == '8' )
						hira.append ( Glib::ustring ( "はち" ) );
					if ( cifre[n] == '9' )
						hira.append ( Glib::ustring ( "きゅう" ) );
					break;
				case 1: // decine
					if ( cifre[n] == '1' )
						hira.append ( Glib::ustring ( "じゅう" ) );
					if ( cifre[n] == '2' )
						hira.append ( Glib::ustring ( "にじゅう" ) );
					if ( cifre[n] == '3' )
						hira.append ( Glib::ustring ( "さんじゅう" ) );
					if ( cifre[n] == '4' )
						hira.append ( Glib::ustring ( "よんじゅう" ) );
					if ( cifre[n] == '5' )
						hira.append ( Glib::ustring ( "ごじゅう" ) );
					if ( cifre[n] == '6' )
						hira.append ( Glib::ustring ( "ろくじゅう" ) );
					if ( cifre[n] == '7' )
						hira.append ( Glib::ustring ( "ななじゅう" ) );
					if ( cifre[n] == '8' )
						hira.append ( Glib::ustring ( "はちじゅう" ) );
					if ( cifre[n] == '9' )
						hira.append ( Glib::ustring ( "きゅうじゅう" ) );
					break;
				case 2: // centinaia
					if ( cifre[n] == '1' )
						hira.append ( Glib::ustring ( "ひゃく" ) );
					if ( cifre[n] == '2' )
						hira.append ( Glib::ustring ( "にひゃく" ) );
					if ( cifre[n] == '3' )
						hira.append ( Glib::ustring ( "さんびゃく" ) );
					if ( cifre[n] == '4' )
						hira.append ( Glib::ustring ( "よんひゃく" ) );
					if ( cifre[n] == '5' )
						hira.append ( Glib::ustring ( "ごひゃく" ) );
					if ( cifre[n] == '6' )
						hira.append ( Glib::ustring ( "ろっぴゃく" ) );
					if ( cifre[n] == '7' )
						hira.append ( Glib::ustring ( "ななひゃく" ) );
					if ( cifre[n] == '8' )
						hira.append ( Glib::ustring ( "はっぴゃく" ) );
					if ( cifre[n] == '9' )
						hira.append ( Glib::ustring ( "きゅうひゃく" ) );
					break;
				case 3: // migliaia
					if ( cifre[n] == '1' )
						hira.append ( Glib::ustring ( "せん" ) );
					if ( cifre[n] == '2' )
						hira.append ( Glib::ustring ( "にせん" ) );
					if ( cifre[n] == '3' )
						hira.append ( Glib::ustring ( "さんぜん" ) );
					if ( cifre[n] == '4' )
						hira.append ( Glib::ustring ( "よんせん" ) );
					if ( cifre[n] == '5' )
						hira.append ( Glib::ustring ( "ごせん" ) );
					if ( cifre[n] == '6' )
						hira.append ( Glib::ustring ( "ろくせん" ) );
					if ( cifre[n] == '7' )
						hira.append ( Glib::ustring ( "ななせん" ) );
					if ( cifre[n] == '8' )
						hira.append ( Glib::ustring ( "はっせん" ) );
					if ( cifre[n] == '9' )
						hira.append ( Glib::ustring ( "きゅうせん" ) );
					break;
				case 4: // decine di migliaia
					if ( cifre[n] == '1' )
						hira.append ( Glib::ustring ( "ばん" ) );
					if ( cifre[n] == '2' )
						hira.append ( Glib::ustring ( "にばん" ) );
					if ( cifre[n] == '3' )
						hira.append ( Glib::ustring ( "さんばん" ) );
					if ( cifre[n] == '4' )
						hira.append ( Glib::ustring ( "よんばん" ) );
					if ( cifre[n] == '5' )
						hira.append ( Glib::ustring ( "ごばん" ) );
					if ( cifre[n] == '6' )
						hira.append ( Glib::ustring ( "ろくばん" ) );
					if ( cifre[n] == '7' )
						hira.append ( Glib::ustring ( "ななばん" ) );
					if ( cifre[n] == '8' )
						hira.append ( Glib::ustring ( "はちばん" ) );
					if ( cifre[n] == '9' )
						hira.append ( Glib::ustring ( "きゅうばん" ) );
					break;
				case 5: // centinaia di migliaia
					if ( cifre[n] == '1' )
						hira.append ( Glib::ustring ( "じゅう" ) );
					if ( cifre[n] == '2' )
						hira.append ( Glib::ustring ( "にじゅう" ) );
					if ( cifre[n] == '3' )
						hira.append ( Glib::ustring ( "さんじゅう" ) );
					if ( cifre[n] == '4' )
						hira.append ( Glib::ustring ( "よんじゅう" ) );
					if ( cifre[n] == '5' )
						hira.append ( Glib::ustring ( "ごじゅう" ) );
					if ( cifre[n] == '6' )
						hira.append ( Glib::ustring ( "ろくじゅう" ) );
					if ( cifre[n] == '7' )
						hira.append ( Glib::ustring ( "ななじゅう" ) );
					if ( cifre[n] == '8' )
						hira.append ( Glib::ustring ( "はちじゅう" ) );
					if ( cifre[n] == '9' )
						hira.append ( Glib::ustring ( "きゅうじゅう" ) );
					break;
				case 6: // milioni
					if ( cifre[n] == '1' )
						hira.append ( Glib::ustring ( "ひゃく" ) );
					if ( cifre[n] == '2' )
						hira.append ( Glib::ustring ( "にひゃく" ) );
					if ( cifre[n] == '3' )
						hira.append ( Glib::ustring ( "さんびゃく" ) );
					if ( cifre[n] == '4' )
						hira.append ( Glib::ustring ( "よんひゃく" ) );
					if ( cifre[n] == '5' )
						hira.append ( Glib::ustring ( "ごひゃく" ) );
					if ( cifre[n] == '6' )
						hira.append ( Glib::ustring ( "ろっぴゃく" ) );
					if ( cifre[n] == '7' )
						hira.append ( Glib::ustring ( "ななひゃく" ) );
					if ( cifre[n] == '8' )
						hira.append ( Glib::ustring ( "はっぴゃく" ) );
					if ( cifre[n] == '9' )
						hira.append ( Glib::ustring ( "きゅうひゃく" ) );
					break;
				case 7: // decine di milioni
					if ( cifre[n] == '1' )
						hira.append ( Glib::ustring ( "せん" ) );
					if ( cifre[n] == '2' )
						hira.append ( Glib::ustring ( "にせん" ) );
					if ( cifre[n] == '3' )
						hira.append ( Glib::ustring ( "さんぜん" ) );
					if ( cifre[n] == '4' )
						hira.append ( Glib::ustring ( "よんせん" ) );
					if ( cifre[n] == '5' )
						hira.append ( Glib::ustring ( "ごせん" ) );
					if ( cifre[n] == '6' )
						hira.append ( Glib::ustring ( "ろくせん" ) );
					if ( cifre[n] == '7' )
						hira.append ( Glib::ustring ( "ななせん" ) );
					if ( cifre[n] == '8' )
						hira.append ( Glib::ustring ( "はっせん" ) );
					if ( cifre[n] == '9' )
						hira.append ( Glib::ustring ( "きゅうせん" ) );
					break;
			}
			n++;
		} while ( n < nc );
		if ( val == 0 )
			hira.append ( Glib::ustring ( "ぜろ" ) );
	}
}
